package top.wangjiu.java.jvm;

import top.wangjiu.java.jvm.accessflag.FieldAccessFlag;
import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;

public class FieldInfo {

    private short index;
    private short accessFlags;
    private short nameIndex;
    private short  descriptorIndex;
    private short  attributesCount;
    private final AttributeInfo[] attributes;

    public FieldInfo(short index,short accessFlags,short nameIndex,short descriptorIndex,AttributeInfo[] attributes){
        this.index = index;
        this.accessFlags = accessFlags;
        this.nameIndex = nameIndex;
        this.descriptorIndex = descriptorIndex;
        this.attributesCount = (short) attributes.length;
        this.attributes = attributes;
    }

    public String toString(ConstantPoolInfo[] poolInfos){
        StringBuilder sb = new StringBuilder("\t["+index+"] " + poolInfos[nameIndex].toString(poolInfos));
        sb.append("\n\t\tdescriptor: "+poolInfos[descriptorIndex].toString(poolInfos));
        sb.append("\n\t\tflags:");
        for (FieldAccessFlag fieldAccessFlag : FieldAccessFlag.parseAccessFlags(accessFlags)) {
            sb.append(" "+fieldAccessFlag.name());
        }
        sb.append("\n\t\tAttributes count:"+ attributesCount);
        for (AttributeInfo attribute : attributes) {
            sb.append("\n\t\t\t"+attribute.toString(poolInfos));
        }
        return sb.toString();
    }
}
